// main.js
// 主入口，负责初始化各模块
console.log('main.js开始加载 - 立即执行');

// 所有 import 语句必须在文件顶部
import { Pendulum } from './pendulum.js';
import { Camera } from './camera.js';
import { ChartModule } from './chart.js';
import { ExportModule } from './export.js';
import { Utils } from './utils.js';
import { pendulumWebSocket } from './websocket-client.js';

console.log('main.js模块导入完成');
console.log('ExportModule导入状态:', typeof ExportModule);

window.PendulumApp = {
  Pendulum,
  Camera,
  Chart: ChartModule,
  Export: ExportModule,
  Utils,
  WebSocket: pendulumWebSocket,
  // 添加调试信息
  debugModules() {
    console.log('Pendulum模块:', this.Pendulum);
    console.log('Camera模块:', this.Camera);
    console.log('Chart模块:', this.Chart);
    console.log('Export模块:', this.Export);
    console.log('Utils模块:', this.Utils);
    console.log('WebSocket模块:', this.WebSocket);
  },
  init() {
    console.log('PendulumApp.init()开始');
    try {
      this.debugModules();
    this.Pendulum.init();
      console.log('Pendulum.init()完成');
    this.Camera.init();
      console.log('Camera.init()完成');
    this.Chart.init();
      console.log('Chart.init()完成');
      console.log('准备调用Export.init()');
    this.Export.init();
      console.log('Export.init()完成');
      console.log('PendulumApp.init()完成');
    } catch (error) {
      console.error('PendulumApp.init()执行过程中出错:', error);
    }
  }
};

window.addEventListener('DOMContentLoaded', () => {
  console.log('DOMContentLoaded事件触发');
  // 延迟一点时间确保DOM完全加载
  setTimeout(() => {
    try {
      console.log('准备调用PendulumApp.init()');
  PendulumApp.init();
      console.log('PendulumApp.init()调用完成');
    } catch (error) {
      console.error('PendulumApp.init()调用失败:', error);
    }
  }, 100);

  // 设置WebSocket连接状态指示器
  const syncStatus2 = document.getElementById('syncStatus2');
  pendulumWebSocket.onConnect(() => {
    if (syncStatus2) {
      syncStatus2.innerHTML = '🟢 WebSocket已连接';
      syncStatus2.className = 'status-value running';
    }
    console.log('WebSocket连接已建立');
    
    // 连接成功后立即请求数据
    pendulumWebSocket.requestState();
    pendulumWebSocket.requestAngleHistory();
    pendulumWebSocket.requestPositionHistory();
    pendulumWebSocket.requestLogs();
    pendulumWebSocket.requestSerialLogs();
  });
  
  pendulumWebSocket.onDisconnect(() => {
    if (syncStatus2) {
      syncStatus2.innerHTML = '🟡 WebSocket已断开';
      syncStatus2.className = 'status-value waiting';
    }
    console.log('WebSocket连接已断开');
  });
  
  pendulumWebSocket.onError(() => {
    if (syncStatus2) {
      syncStatus2.innerHTML = '❌ WebSocket错误';
      syncStatus2.className = 'status-value error';
    }
  });
  
  // 监听摆状态更新
  pendulumWebSocket.onPendulumState((state) => {
    // 更新FPS和延迟显示
    const fpsDisplay = document.getElementById('fpsDisplay');
    const latencyDisplay = document.getElementById('latencyDisplay');
    
    if (fpsDisplay) {
      const fps = PendulumApp.Pendulum.currentFps || 0;
      fpsDisplay.textContent = Math.round(fps);
      fpsDisplay.className = 'status-value';
      if (fps >= 25) {
        fpsDisplay.classList.add('running');
      } else if (fps >= 15) {
        fpsDisplay.classList.add('waiting');
      } else {
        fpsDisplay.classList.add('error');
      }
    }
    
    if (latencyDisplay) {
      const now = performance.now();
      const latency = now - (state.timestamp || now);
      latencyDisplay.textContent = `${Math.round(latency)}ms`;
      latencyDisplay.className = 'status-value';
      if (latency <= 50) {
        latencyDisplay.classList.add('running');
      } else if (latency <= 150) {
        latencyDisplay.classList.add('waiting');
      } else {
        latencyDisplay.classList.add('error');
      }
    }
  });

  // 定期请求更新数据
  setInterval(() => {
    if (pendulumWebSocket.connected) {
      pendulumWebSocket.requestState();
              }
  }, 100);  // 每100ms请求一次状态更新
  
  setInterval(() => {
    if (pendulumWebSocket.connected) {
      pendulumWebSocket.requestLogs();
      pendulumWebSocket.requestSerialLogs();
    }
  }, 1000);  // 每1秒请求一次日志更新
}); 